import hashlib

from django.db import models

from Apps.common.custommodel import CustomModel
from django.contrib.auth.models import AbstractUser
from django.utils.translation import gettext_lazy as _


class User(AbstractUser, CustomModel):
    username = models.CharField(max_length=150, unique=True, db_index=True, verbose_name="用户账号",
                                help_text="用户账号")
    email = models.EmailField(max_length=255, unique=True, verbose_name="邮箱", null=True, help_text="邮箱")
    mobile = models.CharField(max_length=255, unique=True, verbose_name="电话", null=True, help_text="电话")
    avatar = models.CharField(max_length=255, verbose_name="头像", default="/static/default/avatar/morentouxiang.png", null=True, help_text="头像")
    nickname = models.CharField(max_length=40, verbose_name="昵称", help_text="昵称")

    GENDER_CHOICES = (
        (0, "未知"),
        (1, "男"),
        (2, "女"),
    )
    gender = models.IntegerField(
        choices=GENDER_CHOICES, default=0, verbose_name="性别", null=True, blank=True, help_text="性别"
    )
    USER_TYPE = (
        (0, "后台用户"),
        (1, "前台用户"),
    )
    user_type = models.IntegerField(
        choices=USER_TYPE, default=0, verbose_name="用户类型", null=True, blank=True, help_text="用户类型"
    )

    def set_password(self, raw_password):
        self.password = hashlib.md5(raw_password.encode(encoding="UTF-8")).hexdigest()
        self._password = raw_password

    class Meta:
        db_table = "system_user"
        verbose_name = "用户表"
        verbose_name_plural = verbose_name
        ordering = ("-create_time",)
        unique_together = (("username", "password"),)

    def __str__(self):
        return self.id
